<?php

use App\Wen\Utils\StatisticUtils;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Form;
use Dcat\Admin\Layout\Navbar;
use Dcat\Admin\Widgets\Tooltip;

global $__MINI_GLOBAL_REQUEST_ENV__;
global $__MINI_GLOBAL_IS_ADMIN_SUPER__, $__MINI_GLOBAL_TENANT_ID__;

if($__MINI_GLOBAL_REQUEST_ENV__ == 'web'){
    if( env('APP_ADMIN_PREFIX', 'admin') == 'admin' && env('APP_URL') != 'https://test.minisns.cn' ){
        exit('自定义后台访问入口，请在.env文件把 "<b>APP_ADMIN_PREFIX</b>" 改成 "<b>非admin</b>" 值，比如111,222都行，如果搜索不到，则去init目录里的.env默认文件里复制出来！');
    }
    require app_path('Global/check.php');
}
global $__MINI_GLOBAL_IS_ADMIN_SUPER__,$__MINI_GLOBAL_CURRENT_USER_ID__;
$admin_user = Admin::user();
if($admin_user){
    if(!$__MINI_GLOBAL_IS_ADMIN_SUPER__){
        if($admin_user->isAdministrator()){
            $__MINI_GLOBAL_IS_ADMIN_SUPER__ = true;
        }
    }
    if($admin_user->uid && $admin_user->uid > 0){
        $__MINI_GLOBAL_CURRENT_USER_ID__ = $admin_user->uid;
    }
}
// 禁用谷歌字体
//Admin::asset()->alias('@nunito', []);
// mini-icon
global $__MINI_GLOBAL_REQUEST_ENV__;
$extend_css = '';
if($__MINI_GLOBAL_REQUEST_ENV__ == 'web'){
    $path_arr = [];
    if($__MINI_GLOBAL_TENANT_ID__ === 0){
        $total_todo_arr = StatisticUtils::total_todo_count(true, true);
        if($total_todo_arr[0] > 0){
            $path_arr[] = 'posts/index';
        }
        if($total_todo_arr[1] > 0){
            $path_arr[] = 'commen';
        }
        if($total_todo_arr[2] > 0){
            $path_arr[] = 'circle';
        }
        if($total_todo_arr[3] > 0){
            $path_arr[] = '/withdrawal';
        }
        if($total_todo_arr[4] > 0){
            $path_arr[] = 'usertemp';
        }
        if($total_todo_arr[5] > 0){
            $path_arr[] = '/feedback';
        }
        if($total_todo_arr[6] > 0){
            $path_arr[] = 'authentication';
        }
    }else{
        $total_todo_arr = StatisticUtils::total_tenant_todo_count(true, true);
        if($total_todo_arr[0] > 0){
            $path_arr[] = '/posts/index';
        }
        if($total_todo_arr[1] > 0){
            $path_arr[] = '/commen';
        }
        if($total_todo_arr[2] > 0){
            $path_arr[] = '/circle';
        }
    }
    $updated_icons = \Dcat\Admin\Models\Menu::whereIn('uri', $path_arr)->pluck('parent_id', 'id')->toArray();
    $parent_icon = [];
    foreach ($updated_icons as $k => $value){
        if($extend_css == ''){
            $extend_css .= '.nav-item>a[data-id="'.$k.'"]>p:after';
        }else{
            $extend_css .= ', .nav-item>a[data-id="'.$k.'"]>p:after';
        }
        if(!isset($parent_icon[$value])){
            $parent_icon[$value] = 1;
            $extend_css .= ', .nav-item>a[data-id="'.$value.'"]>p:after';
        }
    }
    if($extend_css){
        $extend_css .= '{ content: ""; position: absolute; background-color: red; width: 7px; height: 7px; border-radius: 50%; margin-left: 7px; top: 10px; }';
    }
}

Admin::css('/assets/css/font_3781365_1nh57c9elz1.css');
Admin::css('/assets/css/font_3975577_lwdk7duaaij.css');
Admin::style('
.main-menu .navbar-header .navbar-brand{display:block;}
.table td,.table thead th{white-space: nowrap!important;}
input { min-width: 50px!important; padding: 4px!important; }.has-icon-left input.form-control{padding-left: 3rem!important;}
.table tr td:first-child, .table tr th:first-child{padding-left:unset!important;}
span.input-group-text { padding: 0 0.5rem!important; }
.col-md-12 { padding-right: 5px!important; padding-left: 5px!important;}.tox.tox-tinymce { min-height: 1100px!important; }
.number-group input { max-width: 50px!important; }
td .select2-selection__rendered{display: inline-grid!important;}
.mini_global_admin_open_not_support_tip{background-color: rgba(255, 0, 0, .6)!important; color: #fff!important; border-radius: 5px!important; padding: 2px 14px!important;margin-right: 5px!important;position: relative!important;display: inline-block!important;}.mini_global_admin_open_not_support_tip.success{background-color: mediumseagreen !important;}
.not_support_tip_code{width: 170px; height: 170px;}
span.select2-dropdown{ z-index: 999999; }
.mini-table-action-up, .mini-table-action-down{margin-top: 10px;}select.form-control { min-height: 250px!important; }
.nav-item>a[data-id="39"]>p:after{ width: 50px; height: 20px; line-height: 20px; border-radius: 7px 7px 7px 0; box-shadow: 0 2px 5px 0 rgb(6 25 32 / 51%); background: #21b978;  position: absolute; z-index: 2; margin-left: 5px; top: 0; font-size: 11px; text-align: center; opacity: .8; color: #FFF; cursor: pointer; content: "发货"; }
.nav-item>a[data-id="44"]>p:after{ width: 50px; height: 20px; line-height: 20px; border-radius: 7px 7px 7px 0; box-shadow: 0 2px 5px 0 rgb(6 25 32 / 51%); background: #fc2c3c; background-image: radial-gradient(9% 82%,#fe576f 0,#fc2c3c 100%); position: absolute; z-index: 2; margin-left: 5px; top: 0; font-size: 11px; text-align: center; opacity: .8; color: #FFF; cursor: pointer; content: "退款"; }
.nav-item>a[data-id="88"]>p:after{ content: ""; position: absolute; background-color: violet; width: 7px; height: 7px; border-radius: 50%; margin-left: 7px; top: 10px; }
.mini-left-menu-wrap { display: flex; }a.mini-menu-item { margin-left: 30px; font-size: 18px; color: #555; }
.CodeMirror { width: 100%; height: 400px; border: 1px solid #eee; }
.sidebar-collapse .mini-left-menu-wrap { display: none; }.sidebar-collapse .search-wrap { max-width: 150px; }.sidebar-collapse span[data-target="#admin-setting-config"] { display: none; }
a.mini-menu-item { color: #555;white-space: nowrap; } .dark-mode a.mini-menu-item { color: #fff; }
.layui-layer .layui-layer-content form .row{margin-left: 0; margin-right: 0}
'.$extend_css);
//Admin::script(<<<JS
//
//JS);
// 扩展Column
Grid\Column::extend('code', function ($v) {
    return "<code>$v</code>";
});
Admin::asset()->alias('@codeMirror', [
    'js' => [
        '/packages/codemirror-5.65.13/lib/codemirror.js',
        '/packages/codemirror-5.65.13/addon/edit/matchbrackets.js',
        '/packages/codemirror-5.65.13/mode/htmlmixed/htmlmixed.js',
        '/packages/codemirror-5.65.13/mode/xml/xml.js',
        '/packages/codemirror-5.65.13/mode/javascript/javascript.js',
        '/packages/codemirror-5.65.13/mode/css/css.js',
        '/packages/codemirror-5.65.13/mode/clike/clike.js',
        '/packages/codemirror-5.65.13/mode/php/php.js',
    ],
    'css' => '/packages/codemirror-5.65.13/lib/codemirror.css',
]);

Form::extend('phpEditor', \App\Admin\Extensions\Form\PhpEditor::class);
Form::extend('htmlEditor', \App\Admin\Extensions\Form\HtmlEditor::class);

Grid::resolving(function (Grid $grid) {
    if (! request('_row_')) {
        $grid->tableCollapse();
//        $grid->tools(new App\Admin\Grid\Tools\SwitchGridMode());
    }
});

Admin::navbar(function (Navbar $navbar) use ($__MINI_GLOBAL_TENANT_ID__, $__MINI_GLOBAL_IS_ADMIN_SUPER__){

//    $navbar->left('html...');

    if($__MINI_GLOBAL_TENANT_ID__ == 0){
        global $__MINI_GLOBAL_PAYCODE_0__;
        Tooltip::make('.mini-menu-item.demo')
            ->bottom()
            ->title('<span class="img-wrap"><img class="not_support_tip_code" src="https://img.mini.minisns.cn/images/hbx/minipro.jpg"></span>');
        Tooltip::make('.mini-menu-item.author')
            ->bottom()
            ->title('<span class="img-wrap"><img class="not_support_tip_code" src="https://img.mini.minisns.cn/images/mmqrcode1677495496626%281%29.png"></span>');

        if( (env('APP_URL') == 'https://test.minisns.cn' && $__MINI_GLOBAL_IS_ADMIN_SUPER__) || !$__MINI_GLOBAL_PAYCODE_0__){
            $navbar->left('<div class="mini-left-menu-wrap">
<a class="mini-menu-item" href="https://doc.minisns.cn/" target="_blank">文档</a>
<a class="mini-menu-item" href="https://www.iconfont.cn/manage/index" target="_blank">Iconfont</a>
<a class="mini-menu-item demo" target="_blank">演示</a>
<a class="mini-menu-item author" target="_blank">作者</a>
</div>');
        }

        if($__MINI_GLOBAL_IS_ADMIN_SUPER__){
            $navbar->right(view('admin.system.search'));
        }
    }

});

if($__MINI_GLOBAL_TENANT_ID__ == 0){
    admin_inject_section(Admin::SECTION['NAVBAR_USER_PANEL'], function () use ($admin_user){
        global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
        return view('admin.partials.navbar-user-panel', ['user' => $admin_user, 'is_super' => $__MINI_GLOBAL_IS_ADMIN_SUPER__]);
    });
}

Admin::js('/assets/js/mini-admin-1.10.js');
